#######################
# Make Fig S8
#######################

rm(list=ls())
library(ggplot2)
library(data.table)

#Read in plotting data
path <- "~/Dropbox/CovidTurnout/replication"
plot_data <- fread(paste0(path, "/original_data/no_excuse_turnout_plot_data.csv"))

#Plot
p <- ggplot(data = plot_data, 
       aes(x = age, y = value)) + 
  geom_line(alpha = 0.2, aes(group = state)) +
  stat_summary_bin(fun.y='mean', bins=20,
                   color='black', size=2, geom='point') +
  stat_summary_bin(fun.y='mean', bins=20,
                   color='black', geom='line') +
  facet_wrap(.~label, scales = "free") +
  coord_cartesian(ylim = c(0,1)) +
  theme_classic() + 
  theme(panel.grid.minor = element_line(colour="grey", 
                                        size=0.5, linetype="dotted"),
        strip.background = element_blank(),
        strip.text = element_text(size = 10, face = "bold"))+
  ylab(NULL) +
  scale_x_continuous(name = "Age at Election")+
  geom_vline(xintercept = 65, linetype = "dashed", color = "red")+
  labs(caption = paste0("Average over states shown in bold. States included: ",
                        paste0(unique(plot_data$state), collapse = ", "), "."))

#Save plot
ggsave(paste0(path, "/output/no_excuse_turnout.pdf"), p, width = 6, height = 2.7)
